package com.igeek.controller;

import com.igeek.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * *User 訫
 * *Description TODO
 * *Date 2024/8/16
 **/
@Slf4j
@Controller
public class UserController {
    //显示login.html页面
    @GetMapping("/login.html")
    public String showLogin(){
        return "login";
    }

    //用户登录
    @PostMapping("/login")
    public String login(
            User user,
            HttpSession session,
            Model model
    ){
        if(!StringUtils.hasLength(user.getUsername()) || !StringUtils.hasLength(user.getPassword())){
            log.info("用户名和密码不能为空:user:{}",user);
            model.addAttribute("errorMsg","用户名和密码不能为空");
            return "login";
        }
        if(user.getUsername().equals("admin") && user.getPassword().equals("123")){
            session.setAttribute("user",user);
            return "redirect:/main.html";
        }
        else {
            log.info("用户名和密码错误:user:{}",user);
            model.addAttribute("errorMsg","用户名和密码错误");
            return "login";
        }
    }

    //显示main.html
    @GetMapping("/main.html")
    public String showMain(){
        return "main";
    }

    //用户登出
    @GetMapping("/logout")
    public String handleLogOut(HttpSession session){
        //情况session中所有的信息
        session.invalidate();
        return "redirect:/login.html";
    }

    //显示basic_table.html
    @GetMapping("/basic_table.html")
    public String showBasicTable(Model model){
        List<User> users = new ArrayList<>();
        users.add(new User("张三","123456","/pic/1.jpg"));
        users.add(new User("李四","456789","/pic/2.jpg"));
        users.add(new User("王五","654111","/pic/3.jpg"));
        model.addAttribute("users",users);
        return "basic_table";
    }

    //显示form_layouts.html
    @GetMapping("/form_layouts.html")
    public String showFormLayouts(){
        return "form_layouts";
    }

    //文件上传
    @PostMapping("/upload")
    public String fileUpload(
            User user,
            @RequestPart("file") MultipartFile file,
            @RequestPart("files") List<MultipartFile> files
    ) throws IOException {
        //单个文件上传
        String filename = file.getOriginalFilename();
        String newFilename = UUID.randomUUID().toString() + filename.substring(filename.indexOf("."));
        file.transferTo(new File("D:\\极客营133\\spring_boot\\1.SpringBoot\\pic\\", newFilename));
        user.setPicture("/pic/"+newFilename);
        //多个文件上传
        String oldFilename;
        String newFilename1;
        for (MultipartFile everyF:files
             ) {
            oldFilename = everyF.getOriginalFilename();
            newFilename1 = UUID.randomUUID().toString() + oldFilename.substring(oldFilename.indexOf("."));
            everyF.transferTo(new File("D:\\极客营133\\spring_boot\\1.SpringBoot\\pic\\", newFilename1));
        }
        return "redirect:/main.html";
    }
}
